package com.example.ekupiandroid;

import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import com.example.ekupiandroid.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegistracijaActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registracija);
		
		final EditText etUser, etIme,etPrezime,etAdresa,etBrTel;
		final EditText etPass;
		Button bRegistracija;
		
		etUser = (EditText)findViewById(R.id.etUser);
		etIme = (EditText)findViewById(R.id.etIme);
		etPrezime = (EditText)findViewById(R.id.etPrezime);
		etAdresa = (EditText)findViewById(R.id.etAdresa);
		etBrTel = (EditText)findViewById(R.id.etBrTel);
		etPass = (EditText)findViewById(R.id.etPass);
		
		bRegistracija = (Button)findViewById(R.id.bPotvrdi);
		
		bRegistracija.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Thread t = new Thread (new Runnable() {
					
					public void run() {
				
				
				HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                final JSONObject jason = new JSONObject();

                try {
                    HttpPut post = new HttpPut("http://10.0.2.2:8080/eKupovina/webservices/korisnici/korisnik/novi");
                    jason.put("ime", etIme.getText().toString());
					jason.put("prezime", etPrezime.getText().toString());
					jason.put("adresa", etAdresa.getText().toString());
					jason.put("telefon", etBrTel.getText().toString());
					jason.put("username", etUser.getText().toString());
					jason.put("password", etPass.getText().toString());
                    StringEntity se = new StringEntity( jason.toString()); 
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);

                    
                    if(response!=null){
                        InputStream in = response.getEntity().getContent(); //Get the data in the entity
                    }

                } catch(Exception e) {
                    e.printStackTrace();
                    //createDialog("Error", "Cannot Estabilish Connection");
                }
						
					}
				});
				t.start();
				Toast.makeText(RegistracijaActivity.this, "Registracija uspješna! Prijavite se sa kreiranim korisničkim računom!", Toast.LENGTH_LONG).show();
				Intent Login = new Intent(getBaseContext(), Login.class);
				startActivity(Login);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.registracija, menu);
		return true;
	}

}
